package com.zhfi.dataapply.schedule.reader.chaincode;

import com.alibaba.fastjson.JSON;
import com.zhfi.dataapply.entity.TzResponse;
import com.zhfi.dataapply.entity.data.po.chaincode.CompanyNumTreePO;
import com.zhfi.dataapply.entity.data.vo.chaincode.CompanyNumTreeVO;
import com.zhfi.dataapply.service.CVSourceUrlService;
import com.zhfi.dataapply.service.ForwardService;
import com.zhfi.dataapply.service.chaincode.OriginChainCodeListService;
import lombok.Getter;
import org.springframework.batch.item.ItemReader;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.*;


@Component
public class CompanyNumTreeReader implements ItemReader<CompanyNumTreePO> {

    private static String URL_ID = "12.1";

    /**
     * 产业链列表中的偏移量
     */
    private int chainListSiteNum;

    private List<String> originChainCodeList;
    private List<CompanyNumTreePO> chainList;
    private LinkedList<CompanyNumTreeVO> tmpChainList;

    @Getter
    private String url;

    @Autowired
    private CVSourceUrlService cvSourceUrlService;

    @Autowired
    private OriginChainCodeListService originChainCodeListService;

    @Autowired
    private ForwardService forwardService;

    public void resetUrl() {
        this.url = cvSourceUrlService.getUrlById(URL_ID);
    }

    public void resetOriginChainCodeList() {
        this.originChainCodeList = originChainCodeListService.getOriginChainCodeList();
    }

    @Override
    public CompanyNumTreePO read() {
        if (chainListSiteNum >= chainList.size()) {
            return null;
        }
        return chainList.get(chainListSiteNum ++);
    }

    public void reset(){
        chainList = new ArrayList<>();
        tmpChainList = new LinkedList<>();
        chainListSiteNum = 0;
        resetUrl();
        resetOriginChainCodeList();
        resetChainList();
    }

    private Map<String, Object> getParams(String chainCode) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("chainCode", chainCode);
        return params;
    }

    private void resetChainList() {
        for (String originChainCode : originChainCodeList) {
            TzResponse tzResponse = forwardService.requestForwarder(url, getParams(originChainCode));
            if (null == tzResponse || CollectionUtils.isEmpty(tzResponse.getData())) {
                continue;
            }
            List<CompanyNumTreeVO> companyNumTreeVOList = JSON.parseArray(JSON.toJSONString(tzResponse.getData()), CompanyNumTreeVO.class);
            if (!CollectionUtils.isEmpty(companyNumTreeVOList)) {
                CompanyNumTreeVO vo = companyNumTreeVOList.get(0);
                tmpChainList.add(vo);
                while (tmpChainList.size() > 0) {
                    CompanyNumTreeVO peek = tmpChainList.peek();
                    CompanyNumTreePO po = new CompanyNumTreePO();
                    BeanUtils.copyProperties(peek, po);
                    chainList.add(po);
                    List<CompanyNumTreeVO> childrenNodes = peek.getChildrenNode();
                    if (!CollectionUtils.isEmpty(childrenNodes)) {
                        for (CompanyNumTreeVO child : childrenNodes) {
                            child.setFatherNodeCode(peek.getChainCode());
                            tmpChainList.add(child);
                        }
                    }
                    tmpChainList.remove();
                }
            }
        }
    }
}